
clear
do "...\First.do"
********************************************************************************
use "$work\analysis_sample.dta"
keep if inrange(age,40,70)

g help=1 if inrange(timing,-4,0)
bys pnr: egen N_pre_2=total(help)
tab N_pre_2
keep if N_pre_2==5


** Balanced sample
bys pnr: g N_pnr=_N
tab N_pnr
g balance=(N_pnr==10)

* Stay with the same GP
g stay_5_years=(new_gp==ydernr & timing==5)
tab stay_5_years if timing==5
bys pnr: egen max_5years=max(stay_5_years)

drop if timing>3
drop if timing<-3

replace statins=. if male==0
replace death_CVC=. if male==0


********************************************************************************
*** DDD
********************************************************************************
cap drop interaction2 interaction3 ddd3
g interaction2=low_ses*help3
g interaction3=low_ses*post
g ddd3=low_ses*help3*post

label var help3 "PCP low SES"
label var post "Post"
label var low_ses "Patient low SES"
label var interaction "PCP low SES x Post"
label var interaction2 "PCP low SES x Patient low SES"
label var interaction3 "Patient low SES x Post"
label var ddd3 "PCP low SES x Patient low SES x Post"



********************************************************************************
* Table A2 - Ethnic Danish Sample
********************************************************************************

sum death if dk==1 & post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if dk==1 & post==1 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0 &  dk==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1 & dk==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableA2.doc", replace tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')



sum death_CVC if dk==1 & post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death_CVC if dk==1 & post==1 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death_CVC if post==1 & help3==0 &  dk==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe death_CVC help3 low_ses interaction2 $patient_X $gp_X if post==1 & dk==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableA2.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')

global patient_X "i.age married"


foreach var of varlist N_visits statins diabetes_control ACSC_COPD {

sum `var' if dk==1 & post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum `var' if dk==1 & post==0 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest `var' if post==1 & help3==0 &  dk==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe `var' post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X  if dk==1, a(id year) cluster(id)

outreg2 using "$table\TableA2.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')


}




********************************************************************************
* Table A8 - Balanced sample and sample of patients staying with the same PCP
********************************************************************************
* Panel A
sum N_visits if post==0 &  balance==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum N_visits if post==0 & low_ses==1 &  balance==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest N_visits if post==1 & help3==0 &  balance==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe N_visits post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X  if balance==1, a(id year) cluster(id) 


outreg2 using "$table\TableA8_PanelA.doc", replace tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') 

foreach var of varlist statins diabetes_control ACSC_COPD {

sum `var' if balance==1 & post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum `var' if post==0 & low_ses==1 & balance==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest `var' if post==1 & help3==0 &  balance==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe `var' post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X  if balance==1, a(id year) cluster(id) 

outreg2 using "$table\TableA8_PanelA.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') 

}


** Panel B
sum N_visits if max_5years==1 & post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum N_visits if post==0 & low_ses==1 & max_5years==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest N_visits if post==1 & help3==0 &  max_5years==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe N_visits post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X  if max_5years==1, a(id year) cluster(id)

outreg2 using "$table\TableA8_PanelB.doc", replace tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') 


foreach var of varlist statins diabetes_control ACSC_COPD {

sum `var' if max_5years==1 & post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum `var' if post==0 & low_ses==1 & max_5years==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest `var' if post==1 & help3==0 &  max_5years==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe `var' post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X  if max_5years==1, a(id year) cluster(id) 

outreg2 using "$table\TableA8_PanelB.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')
}









********************************************************************************
* Table A3 - Alternative SES aggregations
********************************************************************************

* Panel A
cap drop help4
cap drop help 

** Minimum function of PCP's SES at the clinic level
g help=yder_ses_min if timing==1
bys pnr min_year: egen help4=max(help)

drop interaction
g interaction=help4
replace interaction=0 if post==0
drop help

cap drop interaction2 interaction3 ddd3
g interaction2=low_ses*help4
g interaction3=low_ses*post
g ddd3=low_ses*help4*post




label var help4 "New PCP low-SES"
label var post "Post"
label var low_ses "Patient low-SES"
label var interaction "New PCP low-SES x Post"
label var interaction2 "New PCP low-SES x Patient low-SES"
label var interaction3 "Patient low-SES x Post "
label var ddd3 "New PCP low-SES x Patient low-SES x Post"

global patient_X "male i.age non_dk married"

sum death if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help4==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help4 low_ses interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)


outreg2 using "$table\TableA3_panelA.doc", replace tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')

* Death CVC
sum death_CVC if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death_CVC if post==1 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death_CVC if post==1 & help4==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death_CVC help4 low_ses interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableA3_panelA.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')



global patient_X "i.age married"

foreach var of varlist N_visits statins diabetes_control ACSC_COPD {

sum `var' if post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum `var' if post==0 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest `var' if post==1 & help3==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe `var' post help4 low_ses interaction interaction2 interaction3 ddd3 $patient_X, a(id year) cluster(id) 

outreg2 using "$table\TableA3_panelA.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')


}




* Panel B
cap drop help4
g help=yder_ses_mean if timing==1
bys pnr min_year: egen help4=max(help)
drop interaction
g interaction=help4
replace interaction=0 if post==0
drop help

cap drop interaction2 interaction3 ddd3
*g interaction=help3*post
g interaction2=low_ses*help4
g interaction3=low_ses*post
g ddd3=low_ses*help4*post

tab help4
sum help4
tab help3



label var help4 "New PCP low-SES"
label var post "Post"
label var low_ses "Patient low-SES"
label var interaction "New PCP low-SES x Post"
label var interaction2 "New PCP low-SES x Patient low-SES"
label var interaction3 "Patient low-SES x Post"
label var ddd3 "New PCP low-SES x Patient low-SES x Post"

global patient_X "male i.age non_dk married"

sum death if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help4==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help4 low_ses interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableA3_panelB.doc", replace tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')


* Death CVC


sum death_CVC if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death_CVC if post==1 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death_CVC if post==1 & help4==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death_CVC help4 low_ses interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableA3_panelB.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')



foreach var of varlist N_visits statins diabetes_control ACSC_COPD {

sum `var' if post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum `var' if post==0 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest `var' if post==1 & help4==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe `var' post help4 low_ses interaction interaction2 interaction3 ddd3 $patient_X, a(id year) cluster(id) 

outreg2 using "$table\TableA3_panelB.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')

}


********************************************************************************
* Table A4 - Non-missing SES
********************************************************************************


sum death if _unknown_new2==0 & post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if _unknown_new2==0 & post==1 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0 &  _unknown_new2==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1 & _unknown_new2==0, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableA4.doc", replace tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5)  addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')

* Death CVC
sum death_CVC if _unknown_new2==0 & post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death_CVC if _unknown_new2==0 & post==1 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death_CVC if post==1 & help3==0 &  _unknown_new2==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death_CVC help3 low_ses interaction2 $patient_X $gp_X if post==1 & _unknown_new2==0, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableA4.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')


foreach var of varlist N_visits statins diabetes_control ACSC_COPD {

sum `var' if _unknown_new2==0 & post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum `var' if _unknown_new2==0 & post==0 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest `var' if post==1 & help3==0 &  _unknown_new2==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe `var' post help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X  if _unknown_new2==0, a(id year) cluster(id) 

outreg2 using "$table\TableA4.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0')

}

